/**
 * author: 何其沆(pooky)
 * desc: Vuex - ExistingClients模块
 * date: 2021-11-19
 */
import { post } from '@/utils/request.js'
import api from '@/config/index.js'

export default {
  state: () => {
    return {
      // Existing Clients列表
      tableList: {},
      // 管理端Existing Clients列表
      adminTableList: {}
    }
  },
  mutations: {
    /**
     * Existing Clients列表
     * @param state
     * @param tableList
     */
    TABLE_LIST(state, tableList) {
      state.tableList = tableList
    },
    /**
     * 管理端Existing Clients列表
     * @param state
     * @param adminTableList
     * @constructor
     */
    ADMIN_TABLE_LIST(state, adminTableList) {
      state.adminTableList = adminTableList
    }
  },
  actions: {
    /**
     * Existing Clients列表
     * @param commit
     * @param data
     * @returns {Promise<void>}
     */
    async setTableList({ commit }, data) {
      await post(api.API_VERSION + '/ExistingClients/QueryPages', data, false).then(res => {
        if (res.code === 2000) {
          if (res.data) {
            const result = res.data
            for (let i = 0; i < result.length; i++) {
              const tags = []
              const item = result[i]
              if (item.status === 11) {
                if (item.isActive === true) {
                  tags.push('Active')
                } else {
                  tags.push('Inactive')
                }
              } else {
                tags.push(item.clientPhaseName)
              }
              tags.push(item.clientKindName.slice(0, 1))
              if (item.businessDirectionName.startsWith('Lo')) {
                tags.push('L10n')
              } else if (item.businessDirectionName.startsWith('AI')) {
                tags.push('Data')
              }
              result[i].tags = tags
              result[i].key = i
            }
          }
          commit('TABLE_LIST', res)
        }
      })
    },
    /**
     * 管理端Existing Clients列表
     * @param commit
     * @param data
     * @returns {Promise<void>}
     */
    async setAdminTableList({ commit }, data) {
      await post(api.API_VERSION + '/Admin/ExistingClients/QueryPages', data, false).then(res => {
        if (res.code === 2000) {
          if (res.data) {
            const result = res.data
            for (let i = 0; i < result.length; i++) {
              const tags = []
              const item = result[i]
              if (item.status === 11) {
                if (item.isActive === true) {
                  tags.push('Active')
                } else {
                  tags.push('Inactive')
                }
              } else {
                tags.push(item.clientPhaseName)
              }
              tags.push(item.clientKindName.slice(0, 1))
              if (item.businessDirectionName.startsWith('Lo')) {
                tags.push('L10n')
              } else if (item.businessDirectionName.startsWith('AI')) {
                tags.push('Data')
              }
              result[i].tags = tags
              result[i].key = i
            }
          }
          commit('ADMIN_TABLE_LIST', res)
        }
      })
    }
  }
}
